//
// Created by gogoqiu on 2021/10/14.
//

#include "PreferenceData.h"
#include "../model/MyTagApp.h"
#include "InputDialog.h"
//#include "PreferencesFrame.h"
#include "../src/MyObj.h"

//https://forums.wxwidgets.org/viewtopic.php?t=6119
//template<class PreferenceData>
/*
class MyObj : public wxObject
{
public:
    MyObj () { data = nullptr; }
    explicit MyObj (void *in_data) { data = in_data; }
    ~MyObj () override { data = nullptr; }
    void *data;
};
*/

///---------------------PreferenceViewHolder----------------------------------///
//textView handler,
/*
template<class PreferenceData>
void PreferenceViewHolder<PreferenceData>::findViews(){

}

//PreferenceData只是一个虚拟类型
template<class PreferenceData>
void PreferenceViewHolder<PreferenceData>::update(PreferenceData *data) {
    //void* p = (void*)&data;
    //
    wxString value = data->getValue();

    auto * titleCtrl = new wxStaticText( this,  wxID_ANY, data->getValue(),
                                         wxDefaultPosition, wxDefaultSize, 0 );
    auto *object = new MyObj( data );
    //
    Connect(wxEVT_LEFT_DOWN, wxMouseEventHandler( PreferenceViewHolder<PreferenceData>::OnClickItem ),
                           object, this );
}
*/

//Panel onClick
template<class PreferenceData>
void PreferenceViewHolder<PreferenceData>::OnClickItem(wxMouseEvent& event){
    auto *myObj = reinterpret_cast<MyObj *>(event.GetEventUserData());
    auto *preferenceView = reinterpret_cast<PreferenceView*>(myObj->data);
    //
    switch( preferenceView->getType() ){
        case PT_INPUT:

            //?
            auto *dialog = new InputDialog( this, preferenceView );
            if ( dialog->ShowModal() == wxID_OK ){
                if( myTagApp->getPreferenceCore()->PutValue( preferenceView->getKey(), preferenceView->getValue())==0 ){
                    preferenceView->getControl()->SetLabel( preferenceView->getValue() );
                }
            }
            break;
    }
    return;
}

//how to create the PreferenceData
template<class PreferenceData>
PreferenceViewHolder<PreferenceData>::PreferenceViewHolder( MyTagApp *myTagApp, wxWindow* parent, PreferenceData* _data )
        :wxPanel(), myTagApp(myTagApp) , data(_data){
    //this->myTagApp = myTagApp;
    //
    //panel = new wxPanel( parent );
    //auto *panelRootBoxSizer = new wxBoxSizer(wxVERTICAL);
    //panelRootBoxSizer->Add(panel, 0,
    //                       wxEXPAND|wxALL, 15);
    //data.getKey();
    /*
     * android layout template:
     *   ui template
     * findViewById
     * */
    SetBackgroundColour( wxColour( 0, 128, 192 ) );
    auto *innerSizer = new wxBoxSizer(wxVERTICAL);
    //titleSizer = new wxBoxSizer(wxHORIZONTAL);
    //
    auto * titleCtrl = new wxStaticText( this,  wxID_ANY, data->getTitle(),
                                         wxDefaultPosition, wxDefaultSize, 0 );
    //
    innerSizer->Add( titleCtrl, 1,
                     wxALL|wxALIGN_LEFT, 10);
    //last updated value
    auto* statusCtrl = new wxStaticText( this,  wxID_ANY, data->getValue(),
                                         wxDefaultPosition, wxDefaultSize, 0 );
    innerSizer->Add( statusCtrl, 1,
                     wxALL|wxALIGN_CENTER_HORIZONTAL, 4);
    //
    data->setControl(statusCtrl);

    SetSizer( innerSizer );
    Layout();
    //
    auto *object = new MyObj( data );
    //
    Connect(wxEVT_LEFT_DOWN, wxMouseEventHandler( PreferenceViewHolder<PreferenceData>::OnClickItem ),
            object, this );
}

///---------------------PreferenceListAdapter----------------------------------///
void PreferenceListAdapter::onBindViewHolder ( PreferenceViewHolder<PreferenceView>* holder, int position ) {
    //holder->update( &items.at(position) );
}

//重构的机会
void PreferenceListAdapter::onBindViewHolder(PreferenceViewHolder<PreferenceView> *holder,
                                             PreferenceView* data ) {
    //holder->update( data );
}

/*
 * */
PreferenceViewHolder<PreferenceView> *PreferenceListAdapter::onCreateViewHolder( int viewType, PreferenceView* data ) {

    return new PreferenceViewHolder<PreferenceView>( myTagApp, parent, data );
}

/**
 * 都是虚函数找不到
 * 继承类实例化找不到基类的虚函数的实现
 * ../libpreference/libpreference.a(PreferenceData.cpp.o)：在函数‘PreferenceListAdapter::PreferenceListAdapter(MyTagApp*, wxWindow*, wxScrolledWindow*)’中：
 * /media/gogoqiu/Data/init/git_oschina/apps/RapidSVN_cmake/libpreference/PreferenceData.cpp:135：
 *          对‘ListViewAdapter<PreferenceView, PreferenceViewHolder<PreferenceView> >::ListViewAdapter(wxWindow*, wxScrolledWindow*)’未定义的引用
 * ../libpreference/libpreference.a(PreferenceData.cpp.o):
 *          (.data.rel.ro._ZTV15ListViewAdapterI14PreferenceView20PreferenceViewHolderIS0_EE[_ZTV15ListViewAdapterI14PreferenceView20PreferenceViewHolderIS0_EE]+0x18)：
 *          对‘ListViewAdapter<PreferenceView, PreferenceViewHolder<PreferenceView> >::onBindViewHolder(PreferenceViewHolder<PreferenceView>*, int)’未定义的引用
 * ../libpreference/libpreference.a(PreferenceData.cpp.o):
 *          (.data.rel.ro._ZTV15ListViewAdapterI14PreferenceView20PreferenceViewHolderIS0_EE[_ZTV15ListViewAdapterI14PreferenceView20PreferenceViewHolderIS0_EE]+0x20)：
 *          对‘ListViewAdapter<PreferenceView, PreferenceViewHolder<PreferenceView> >::onBindViewHolder(PreferenceViewHolder<PreferenceView>*, PreferenceView*)’未定义的引用
 * ../libpreference/libpreference.a(PreferenceData.cpp.o):
 *          (.data.rel.ro._ZTV15ListViewAdapterI14PreferenceView20PreferenceViewHolderIS0_EE[_ZTV15ListViewAdapterI14PreferenceView20PreferenceViewHolderIS0_EE]+0x28)：
 *          对‘ListViewAdapter<PreferenceView, PreferenceViewHolder<PreferenceView> >::onCreateViewHolder(int)’未定义的引用
 * @param myTagApp
 * @param frame
 */
 /*
PreferenceListAdapter::PreferenceListAdapter( MyTagApp *myTagApp, wxWindow *frame, wxScrolledWindow* listBox )
        : ListViewAdapter( frame , listBox ) {
    this->myTagApp = myTagApp;
    items = myTagApp->getPreferenceCore()->getPreferenceViews();
}
*/
 /*
 PreferenceListAdapter::PreferenceListAdapter(wxWindow *unnamed, wxScrolledWindow *unnamed1, MyTagApp *myTagApp,
                                              wxWindow *frame, wxScrolledWindow *listBox) : ListViewAdapter(unnamed, unnamed1)
 //:ListViewAdapter<PreferenceView, PreferenceViewHolder<PreferenceView>>( frame , listBox )
        {
    this->myTagApp = myTagApp;
    items = myTagApp->getPreferenceCore()->getPreferenceViews();
}
*/
/*
int main(){
    new PreferenceListAdapter<PreferenceData,
}*/
/*
 * notifyDataSetChanged
 *
 */
/*
 * ListViewAdapter::setItemsWithNotify修改为virtual, 本地setItemsWithNotify修改为override
 * 总是报错
 * 调用ListViewAdapter::notifyDataSetChanged();
void PreferenceListAdapter::setItemsWithNotify (  vector<PreferenceView*> infos ) {
    //ListViewAdapter::setItemsWithNotify( infos );
    this->items.clear();
    for( PreferenceView* item: infos ){
        this->items.push_back( item );
    }
    //this->items.addAll ( infos );
    ListViewAdapter::notifyDataSetChanged();
};

void PreferenceListAdapter::appendItemsWithNotify (  vector<PreferenceView*> infos ) {
    ListViewAdapter::appendItemsWithNotify( infos );
};
*/

/**
 * 怎么会要求ListViewAdapter<PreferenceView, PreferenceViewHolder<PreferenceView> >::onBindViewHolder(PreferenceViewHolder<PreferenceView>*, int)的实现？
 * 不是<double, int>吗
 * ../libpreference/libpreference.a(PreferenceData.cpp.o)：在函数‘Test::Test()’中：
/media/gogoqiu/Data/init/git_oschina/apps/RapidSVN_cmake/libpreference/PreferenceData.cpp:194：
 对‘ListViewAdapter<int, double>::ListViewAdapter(wxWindow*, wxScrolledWindow*)’未定义的引用

../libpreference/libpreference.a(PreferenceData.cpp.o):
 (.data.rel.ro._ZTV15ListViewAdapterI14PreferenceView20PreferenceViewHolderIS0_EE[_ZTV15ListViewAdapterI14PreferenceView20PreferenceViewHolderIS0_EE]+0x18)：
 对‘ListViewAdapter<PreferenceView, PreferenceViewHolder<PreferenceView> >::onBindViewHolder(PreferenceViewHolder<PreferenceView>*, int)’未定义的引用
../libpreference/libpreference.a(PreferenceData.cpp.o):

 (.data.rel.ro._ZTV15ListViewAdapterI14PreferenceView20PreferenceViewHolderIS0_EE[_ZTV15ListViewAdapterI14PreferenceView20PreferenceViewHolderIS0_EE]+0x20)：
 对‘ListViewAdapter<PreferenceView, PreferenceViewHolder<PreferenceView> >::onBindViewHolder(PreferenceViewHolder<PreferenceView>*, PreferenceView*)’未定义的引用

../libpreference/libpreference.a(PreferenceData.cpp.o):
 (.data.rel.ro._ZTV15ListViewAdapterI14PreferenceView20PreferenceViewHolderIS0_EE[_ZTV15ListViewAdapterI14PreferenceView20PreferenceViewHolderIS0_EE]+0x28)：
 对‘ListViewAdapter<PreferenceView, PreferenceViewHolder<PreferenceView> >::onCreateViewHolder(int)’未定义的引用
 * 继承，然后构造就产生甚多问题
 */
//template<double, int>
class Test :public ListViewAdapter< int  , double >{
public:
    /*
    Test():ListViewAdapter<int, double>(nullptr, nullptr){

    }*/

    int getItemViewType ( int position ) override
    {
        //return super.getItemViewType(position);
        return 0;
    }

    void onBindViewHolder ( double* holder, int position ) override{

    };

    void onBindViewHolder( double *holder,
                                   int* date ) override{

    };
    //wxFrame
    //ui thread side
    double* onCreateViewHolder ( int viewType, int *data ) override{
        return nullptr;
    };
};

template< class T, class V >
class Temp{
public:
    Temp( T data ){
        this->data = data;
    }
    void setData( T data ){
        this->data = data;
    }
    virtual void test(){
        //do something
    }
protected:
    T data;
};

class ChildTemp: public Temp<int, double>{
public:
    ChildTemp():Temp(3){

    }
    /*
    void test() override{
        //do something
    }*/
protected:
};

class TestVector: public vector<int>{
public:
    TestVector(){
        this->push_back(1);
        this->push_back(2);
        this->push_back(3);
    }
};

static void test(){
    new ListViewAdapter<wxString, PreferenceItem>( nullptr , nullptr );
    //new Test();
    new TestVector();
    (new ChildTemp())->test();
}